<template>
  <div>
    <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="100px" class="demo-dynamic">
      <el-form-item
          v-for="(domain, index) in dynamicValidateForm.domains"
          :label="'演员信息' + index"
          :key="domain.key"
          :prop="'domains.' + index + '.value'"
      >
        <span style="margin-left: 50px">演员：</span><el-select v-model="domain.actorId" filterable placeholder="请选择" style="width: 120px">
                <el-option
                    v-for="item in actorList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id">
                </el-option>
             </el-select>
        <span style="margin-left: 50px">饰演：</span><el-input v-model="domain.role" placeholder="请输入饰演" style="width: 120px"></el-input>
        <span style="margin-left: 50px">简介：</span>
        <el-input
            type="textarea"
            autosize
            placeholder="请输入内容"
            v-model="domain.introduce"
            style="width: 600px"
        >
        </el-input>
        <span style="margin-left: 50px">排序：</span><el-input placeholder="排序" v-model="domain.sort" style="width: 120px"></el-input>
        <el-button @click.prevent="removeDomain(domain)" style="margin-left: 50px">删除</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('dynamicValidateForm')">提交</el-button>
        <el-button @click="addDomain">新增域名</el-button>
        <el-button @click="resetForm('dynamicValidateForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "MovieAddActorView",
  data() {
    return {
      movieId: this.$route.query.movieId,
      actorList: [],
      dynamicValidateForm: {
        domains: [{
          actorId: '',
          role: '',
          introduce: '',
          sort: ''
        }]
      }
    };
  },
  created() {
    this.getActorList(),
    this.getMovieActorList()
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.$router.push({path: "/movie/add", query: {
              movieActorList: this.dynamicValidateForm.domains,
              id: this.movieId
            }})
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    removeDomain(item) {
      var index = this.dynamicValidateForm.domains.indexOf(item)
      if (index !== -1) {
        this.dynamicValidateForm.domains.splice(index, 1)
      }
    },
    addDomain() {
      this.dynamicValidateForm.domains.push({
        value: '',
        key: Date.now()
      });
    },
    getActorList() {
      this.axios.get("manage/actor/getAll").then(response=>{
        if (response.data.code == 200){
          this.actorList = response.data.data;
        }
      })
    },
    getMovieActorList() {
      this.axios.get("/manage/participating/getParticipatingByMovieId",{params: {movieId: this.movieId}}).then(response=>{
        if (response.data.code == 200){
          console.log(response.data.data)
          this.dynamicValidateForm.domains=response.data.data
        }
      })
    }
  }
}
</script>

<style scoped>

</style>